<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%--资金计划汇总台账点击批量审批更新对应流程明细数据并提交流程--%>
<%
    JSONObject dataJson = new JSONObject();
    try {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select a.xglc from (select xglc from uf_zjjhhzb where zt=0 group by xglc) a," +
                "workflow_requestbase b where a.xglc=b.requestid and b.currentnodetype<3");
        int counts = rs.getCounts();
        int succcount = 0;
        while (rs.next()){
            String xglc =Util.null2String(rs.getString("xglc"));
            boolean flag = updateZjjhtbshDtl(xglc);
            if(flag){
               boolean status= autoSubmit(xglc);
               if(status){
                   succcount++;
               }
            }
        }
        dataJson.put("counts",counts);
        dataJson.put("succcount",succcount);
        dataJson.put("failcount",counts-succcount);
    } catch (Exception e) {
        dataJson.put("counts",0);
        dataJson.put("succcount",0);
        dataJson.put("failcount",0);
        e.printStackTrace();
    }
    out.print(dataJson.toJSONString());
%>
<%!
    private static boolean updateZjjhtbshDtl(String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select jhid,cwfhjeyb,cwfhjebb,cwfhrgh,cwfhr,pldb,mxbs from uf_zjjhhzb where xglc=?", requestid);
        int counts = rs.getCounts();
        int count = 0;
        while (rs.next()) {
            String jhid = Util.null2String(rs.getString("jhid"));
            String cwfhjeyb = Util.null2String(rs.getString("cwfhjeyb"));
            String cwfhjebb = Util.null2String(rs.getString("cwfhjebb"));
            String cwfhrgh = Util.null2String(rs.getString("cwfhrgh"));
            String cwfhr = Util.null2String(rs.getString("cwfhr"));
            String pldb = Util.null2String(rs.getString("pldb"));
            String mxbs = Util.null2String(rs.getString("mxbs"));
            String sql = "update " + mxbs + " set cwfhjeyb=?,cwfhjebb=?,cwfhrgh=?,cwfhr=?,pldb=? where jhid=?";
            boolean flag = rs.executeUpdate(sql, cwfhjeyb, cwfhjebb, cwfhrgh, cwfhr, pldb, jhid);
            if (flag) {
                count++;
            }
        }
        return counts == count;
    }

    public static boolean autoSubmit(String requestid){
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select distinct userid,usertype,agenttype,agentorbyagentid from workflow_currentoperator\n" +
                " where (isremark in ('0','1','5','7','8','9') or (isremark='4' and viewtype=0))  and requestid = ?", requestid );
        rs.next();
        int operator = Util.getIntValue( rs.getString("userid"));
        RequestService server = new RequestService();
        RequestInfo requestInfo = server.getRequest(Integer.parseInt(requestid));
        return new RequestService().nextNodeBySubmit(requestInfo, Integer.parseInt(requestid), operator, "");
    }
%>



